<?php
/**
 * @notes:
 * @return
 * @author: admin
 * @date: 2024/9/7
 */


namespace addons\shopro\controller\lucky;


use addons\shopro\controller\Common;
use addons\shopro\service\lucky\LuckyService;
use addons\shopro\service\lucky\RecordService;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\lucky\Agent;
use app\admin\model\shopro\lucky\Auto;
use think\Db;
use think\Exception;

class Lucky extends Common
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /***
     * @notes:获取活动列表
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/8 0:07
     */
    public function getlist()
    {
        $user = auth_user();
        $this->success('获取成功', (new LuckyService())->getList($user));

    }

    /***
     * 预购处理
     * @notes:
     * @author: 小谢
     * @date: 2024/9/9 22:11
     */
    public function setlucky()
    {
        if(sheep_config('shop.basic.order')=='0')
        {
            $this->error('操作不在时间范围内');
        }

        $params = $this->request->only(['number', 'rep_password', 'server_id']);
        $this->svalidate($params,'.server');
        $user = auth_user();

        if((new LuckyService())->setLucky($params,$user)){
            $this->success('预购成功！');
        }

    }

    /***
     * 处理充值申请
     * @notes:
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/10 13:58
     */
    public function payrecord()
    {
        $params = $this->request->only(['images','recharge_money', 'remittance_id', 'online']);//
        $this->svalidate($params,'.payrecord');

        if ($params['recharge_money'] < 3000) {
            error_stop('提交充值，必须大于3000');
        }

        $params['online'] = $params['online'] ?? 0;

        $user = auth_user();
        $res=(new RecordService())->payRecord($params,$user);
        if($res){
            $this->success('申请成功',$res);
        }
    }

    public function getrecordlist()
    {
        $user = auth_user();
        $this->success('获取成功',(new RecordService())->getList($user));
    }

    public function getrecordagent()
    {
        $user = auth_user();
        $type = $this->request->param('type', "all");
        if (!in_array($type, ['all', 'commission', 'team'])) {
            $this->error("");
        }
        $date = $this->request->param('date/a');
        $query=Db::name('shopro_lucky_record_agent')->alias('ra')
            ->join('shopro_lucky_record lr','ra.lucky_record_id=lr.id','left')
            ->join('user us','ra.buy_id=us.id')
            ->where(['lr.status'=>1,'ra.agent_id'=>$user['id']])
            ->whereTime('lr.createtime', 'between', $date)
            ->field('lr.account,us.nickname,us.avatar,lr.createtime,ra.is_commission');
        if($type==='commission'){
            $query->where('ra.is_commission',1);
        }
        if($type==='team'){
            $query->where('ra.is_commission',0);
        }

        $data = $query->paginate($this->request->param('list_rows', 10));
        $this->success('',$data);

    }

    public function getsumcount()
    {
        $user = auth_user();
        $date = $this->request->param('date/a');
        $total_account_all=Db::name('shopro_lucky_record_agent')->alias('ra')
            ->join('shopro_lucky_record lr','ra.lucky_record_id=lr.id','left')
            ->join('user us','ra.buy_id=us.id','left')
            ->where(['lr.status'=>1,'ra.agent_id'=>$user['id']])
            ->whereTime('lr.createtime', 'between', $date)->sum('lr.account');

        $total_account=Db::name('shopro_lucky_record_agent')->alias('ra')
            ->join('shopro_lucky_record lr','ra.lucky_record_id=lr.id','left')
            ->join('user us','ra.buy_id=us.id','left')
            ->where(['lr.status'=>1,'ra.agent_id'=>$user['id'],'ra.is_commission'=>1])
            ->whereTime('lr.createtime', 'between', $date)->sum('lr.account');

        $this->success('',['total_account_all'=>$total_account_all,'total_account'=>$total_account]);
    }

    public function getsumall()
    {
        $user = auth_user();
        $date = $this->request->param('date/a');
        $total_account_all=Db::name('shopro_lucky_record_agent')->alias('ra')
            ->join('shopro_lucky_record lr','ra.lucky_record_id=lr.id','left')
            ->join('user us','ra.buy_id=us.id','left')
            ->where(['lr.status'=>1,'ra.agent_id'=>$user['id']])
            ->sum('lr.account');

        $total_account=Db::name('shopro_lucky_record_agent')->alias('ra')
            ->join('shopro_lucky_record lr','ra.lucky_record_id=lr.id','left')
            ->join('user us','ra.buy_id=us.id','left')
            ->where(['lr.status'=>1,'ra.agent_id'=>$user['id'],'ra.is_commission'=>1])
            ->sum('lr.account');

        $this->success('',['total_account_all'=>$total_account_all,'total_account'=>$total_account]);
    }

    public function setAutoInfo()
    {
        $user = auth_user();
        $params=$this->request->only(['rep_password']);

        $this->svalidate($params,'.auto');

        if ($user->rep_password != md5(md5($params['rep_password']) . $user->salt)) {
            error_stop('支付密码错误！');
        }
        if($user->commission<300){
            error_stop('开通自动预约需要花费300福利券/月');
        }

        Db::startTrans();
        try{
            if(WalletService::change($user,'commission',-300,'auto_buy')){
                $date = new \DateTime();
                $date->modify('+30 day');
                $data=[
                    'user_id'=>$user['id'],
                    'end_time'=>$date->format('Y-m-d H:m:s')
                ];
                $auto=Auto::where(['user_id'=>$user['id']])->find();
                if($auto){
                    $date = new \DateTime($auto['end_time']);
                    $date->modify('+30 day');
                    $data['end_time']=$date->format('Y-m-d H:m:s');
                    if(!Auto::where(['user_id'=>$user['id']])->update($data)){
                        throw new Exception('续费失败');
                    }
                }else{
                    $auto =  new Auto();
                    if(!$auto->insert($data)){
                        throw new Exception('开通失败');
                    }
                }
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            error_stop('开通失败');
        }
        $this->success('开通成功');
    }

    public function getAutoInfo()
    {
        $user = auth_user();
        $auto=Auto::where(['user_id'=>$user['id']])->find();
        $this->success('ok',$auto);

    }

}